/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.shell;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

import org.apache.commons.io.IOUtils;

import com.hyts.shell.cache.ICache;
import com.hyts.shell.cache.ICacheFactory;
import com.hyts.shell.config.SSHConfig;
import com.hyts.shell.executor.EthzExecutor;
import com.hyts.shell.executor.JcraftExecutor;
import com.hyts.shell.executor.base.Executor;
import com.hyts.shell.handler.AbstractStreamHandler;
import com.hyts.shell.handler.BaseProcessStream;
import com.hyts.shell.model.ExecuteDomain;
import com.hyts.shell.model.ResultDomain;
import com.hyts.shell.model.StringResultDomain;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/** 
 * @title ShellExcecutor.java  
 * @package com.hyts.shell.executor  
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月17日 
 * @version V1.0 
 */
public final class SSH{
	
	/**  
	 * <p>SSH处理器操作进行-处理SSH登录</p> 
	 * @fieldName executor
	 * @fieldType Executor
	 */ 
	private Executor<?,?> executor;
	
	/**  
	 * @constructor：SSH    
	 */ 
	public SSH() {
		this.executor = new EthzExecutor();
	}

	/**  
	 * @constructor：SSH  
	 */
	public SSH(Executor<?,?> executor) {
		this.executor = executor;
	}

	/**  
	 * <b>TODO(方法功能的描述)</b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param domain
	 * @param clazz
	 * @return
	 * @exception
	 */ 
	/*public <T> ResultDomain<T> execute(ExecuteDomain domain,Class<T> clazz) {
		if(this.executor instanceof EthzExecutor) {
			((EthzExecutor)this.executor).execute(domain);
		}else if(this.executor instanceof JcraftExecutor){
			return ((JcraftExecutor)this.executor).execute(domain);
		}
		return null;
	} */
	
	/**
	 * @param domain
	 * @return
	 * @exception
	 */ 
	public ResultDomain<String> execute(ExecuteDomain domain) {
		if(this.executor instanceof EthzExecutor) {
			return ((EthzExecutor)this.executor).execute(domain);
		}else if(this.executor instanceof JcraftExecutor){
			return ((JcraftExecutor)this.executor).execute(domain);
		}
		return null;
	}

	/**
	 * @param ip IP地址
	 * @param userName 用户米
	 * @param password 密码
	 * @return
	 */
	public ResultDomain<String> execute(String ip, String userName, String password){
		return execute(new ExecuteDomain(ip,userName,password));
	}

}
